package com.kyzh.core.uis

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.kyzh.core.R
import com.kyzh.core.utils.dip


class   TitleView : LinearLayout {


    private lateinit var close: ImageView
    private lateinit var more: ImageView
    private lateinit var title: TextView
    private var text: String? = null
    private var textColor: Int = Color.BLACK
    private var bgColor: Int = Color.WHITE
    private var moreVisibility: Boolean = false
    private var closeVisibility: Boolean = true
    private var moreDrawable: Int = R.drawable.frag_home_download
    private var moreWidth: Float = 1f
    private var moreHeight: Float = 1f
    private lateinit var titlebar :ConstraintLayout
    constructor(context: Context) : this(context, null, 0)
    constructor(context: Context, attributes: AttributeSet) : this(context, attributes, 0)
    constructor(context: Context, attributes: AttributeSet?, defStyle: Int) : super(context, attributes, defStyle) {
        val typeArray = context.obtainStyledAttributes(attributes, R.styleable.TitleView)
        text = typeArray.getString(R.styleable.TitleView_title) ?: ""
        textColor = typeArray.getColor(R.styleable.TitleView_titleColor, resources.getColor(R.color.font_33))
        bgColor = typeArray.getColor(R.styleable.TitleView_bgColor, Color.WHITE)
        moreVisibility = typeArray.getBoolean(R.styleable.TitleView_moreVisibilty, false)
        closeVisibility = typeArray.getBoolean(R.styleable.TitleView_closeVisibilty, true)
        moreDrawable = typeArray.getResourceId(R.styleable.TitleView_moreDrawable, R.drawable.frag_home_download)
        moreHeight = typeArray.getDimension(R.styleable.TitleView_moreHeight, context.dip(20).toFloat())
        moreWidth = typeArray.getDimension(R.styleable.TitleView_moreHeight, context.dip(20).toFloat())
        initView(context)
    }

    private fun initView(context: Context) {
        LayoutInflater.from(context).inflate(R.layout.titleview, this, true)
        close = findViewById(R.id.close)
        title = findViewById(R.id.tvTitle)
        more = findViewById(R.id.titleMore)
         titlebar = findViewById<ConstraintLayout>(R.id.titleBar)
        titlebar.setBackgroundColor(bgColor)
        close.setOnClickListener { (context as? Activity)?.finish() }
        title.text = text
        more.setImageResource(moreDrawable)
        more.setPadding(((context.dip(40) - moreWidth) / 2).toInt(), ((context.dip(40) - moreHeight) / 2).toInt(), ((context.dip(40) - moreWidth) / 2).toInt(), ((context.dip(40) - moreHeight) / 2).toInt())
        more.visibility = if (moreVisibility) View.VISIBLE else View.GONE
        close.visibility = if (closeVisibility) View.VISIBLE else View.GONE
        title.setTextColor(textColor)

        close.drawable.setTint(textColor)
        more.drawable.setTint(textColor)
    }
    fun setBg(id :Int){
        titlebar.setBackgroundResource(id)
    }
    fun getClose () : ImageView{
        return close
    }

    fun setText(text: String) {
        title.text = text
    }

    fun setOnMoreOnClickListener(listener: SimpleClick.() -> Unit) {
        more.setOnClickListener {
            SimpleClick().listener()
        }
    }

    interface OnClick {
        fun click(v: View)
    }

    class SimpleClick : OnClick {
        private var _OnClick: ((view: View) -> Unit)? = null
        fun onClick(listener: (view: View) -> Unit) {
            _OnClick = listener
        }

        override fun click(v: View) {
            _OnClick?.invoke(v)
        }

    }
}